/**
 * Author: w景洋.
 * Time: 2016/4/19 0019
 */
angular.module('service.shopmenu', [
        "ngResource"
    ])
    .service('ShopMenuService', ShopMenuService);
ShopMenuService.$inject = ['MenuGoodsService','ShopService'];
function ShopMenuService(MenuGoodsService,ShopService) {
    var shop_goods_list = {};
    var order_list = [];
    var menuList = {};
    //根据店铺id获取店铺菜单
    this.getMenuByShopid = function (shopid) {
        if (menuList[shopid]) {
            loadCart(false, "loader");
            announcement_run(true);
            return menuList[shopid];
        }
        var menu = MenuGoodsService.getMenuByShopid(shopid);
        menu.$promise.then(function (goodsList) {
            loadCart(false, "loader");
            announcement_run(true);
            for (var i = goodsList.length - 1; i >= 0; i--) {
                var goodsitem = goodsList[i];
                for (var j = goodsitem.goods.length - 1; j >= 0; j--) {
                    var good = goodsitem.goods[j];
                    if (good.isDisplay != 1) {
                        goodsitem.goods.splice(j, 1)
                    } else {
                        good.num = 0;
                        good.groupid = goodsitem.groupid;
                    }
                    if(good.goodsSkuList.length>0){
                        var goodsSkuList = good.goodsSkuList;
                        var price = goodsSkuList[0].price;
                        var stock = 0;
                        for(var k=0;k<goodsSkuList.length;k++){
                            stock += goodsSkuList[k].quantity;
                            if(goodsSkuList[k].price<price){
                                price=goodsSkuList[k].price;
                            }
                        }
                        if(good.price>price){
                            good.price = price;
                        }
                        good.stock = stock;
                    }
                }
                goodsitem.num = 0;
                var goods = goodsitem.goods;
                if (goodsitem.goods.length == 0) {
                    goodsList.splice(i, 1)
                }
            }
        }, function () {
            loadCart(false, "loader");
            announcement_run(true);
        });
        menuList[shopid] = menu;
        return menu;
    };
    //新增购买
    this.addGood = function (shopid, groupid, goods, sku) {
        if (!shop_goods_list[shopid]) {
            shop_goods_list[shopid] = [];
        }
        var goodsList = shop_goods_list[shopid];
        //shopid 菜单为空，初始化商店菜单
        //if (goodsList == undefined) {
        //    goodsList = menuList[shopid];
        //}
        var goodsitem = null;
        for (var i = 0; i < goodsList.length; i++) {
            //判断当前购买商品是否有sku，有sku，sku需加入判断
            if (goodsList[i].groupid == groupid && goodsList[i].goodsid == goods.goodsid) {
                if(goods.skuid){
                    if (goodsList[i].skuid == goods.skuid) {
                        goodsitem = goodsList[i];
                        break;
                    }
                }else{
                    if (sku) {
                        if (goodsList[i].skuid == sku.skuid) {
                            goodsitem = goodsList[i];
                            break;
                        }
                    } else {
                        goodsitem = goodsList[i];
                        break;
                    }
                }
            }
        }
        if (goodsitem == null) {
            //该商品第一次购买
            var detail = this.getGoodsDetail(shopid, groupid, goods.goodsid, 'add');
            var cartGood = {};
            for (var name in detail.goods) {
                if (typeof detail.goods[name] != "object") {
                    cartGood[name] = detail.goods[name];
                }
            }
            if(goods.goodsSkuList.length==0){
                cartGood.currentFee = goods.price;
                cartGood.skuid = 0;
                cartGood.properties = goods.description;
                cartGood.num = 1;
            }
            else if(goods.goodsSkuList.length==1){
                var goodsSku = goods.goodsSkuList[0];
                cartGood.price = goodsSku.price;
                cartGood.currentFee = goodsSku.price;
                cartGood.skuid = goodsSku.skuid;
                cartGood.properties = goodsSku.properties;
                cartGood.num = 1;
            }
            if (sku) {
                cartGood.price = sku.price;
                cartGood.currentFee = sku.price;
                cartGood.skuid = sku.skuid;
                cartGood.properties = sku.properties;
                cartGood.stock = sku.quantity?sku.quantity:0;
                cartGood.num = 1;
            }
            //判断商品是否是优惠，优惠计算优惠价格
            var goodsPromotionBase = detail.goods.goodsPromotionBase;
            cartGood.discountGoods = false;
            //cartGood.single = false;
            cartGood.cartGoods = true;
            if (goodsPromotionBase) {
                if (goodsPromotionBase.type == 1) {
                    cartGood.currentFee = cartGood.currentFee * goodsPromotionBase.discount / 10;
                } else if (goodsPromotionBase.type == 2) {
                    cartGood.currentFee -= goodsPromotionBase.discount;
                }
                cartGood.skuid = goodsPromotionBase.promotionid;
                cartGood.discountGoods = true;
                //cartGood.single = true;
            }
            shop_goods_list[shopid].push(cartGood);
        } else {
            if(goodsitem.discountGoods&&goodsitem.num>1){
                input_alert("该美食限1份优惠，超过以原价计算哦",3000,"#prompt",100);
            }
            if(sku){
                if (goodsitem.num < sku.quantity) {
                    goodsitem.num++;
                } else {
                    input_alert("超出库存！");
                    return;
                }
            }else{
                if (goodsitem.num < goods.stock) {
                    goodsitem.num++;
                } else {
                    input_alert("超出库存！");
                    return;
                }
            }
            this.getGoodsDetail(shopid, groupid, goods.goodsid, 'add');
        }
    };
    //扫码购买商品
    this.addBarcode = function(shopid,goods){
        if (!shop_goods_list[shopid]) {
            shop_goods_list[shopid] = [];
        }
        var goodsList = shop_goods_list[shopid];
        var goodsitem = null;
        for (var i = 0; i < goodsList.length; i++) {
            //判断当前购买商品是否有sku，有sku，sku需加入判断
            if (goodsList[i].goodsid == goods.goodsid) {
                goodsitem = goodsList[i];
                break;
            }
        }
        if (goodsitem == null) {
            //该商品第一次购买
            var cartGood = {};
            if(goods.goodsSkuList.length==0){
                cartGood.currentFee = goods.price;
                cartGood.skuid = 0;
                cartGood.properties = goods.description;
            }
            else if(goods.goodsSkuList.length==1){
                var goodsSku = goods.goodsSkuList[0];
                cartGood.price = goodsSku.price;
                cartGood.currentFee = goodsSku.price;
                cartGood.skuid = goodsSku.skuid;
                cartGood.properties = goodsSku.properties;
            }
            goods.shopid = shopid;
            goods.currentFee = goods.price;
            goods.images = "";
            goods.state = 0;
            goods.num = 1;
            goods.cartid = "";
            for (var name in goods) {
                //console.log(typeof goods[name]);
                if (typeof goods[name] != "object"&&typeof goods[name]!="function") {
                    cartGood[name] = goods[name];
                }
            }
            //判断商品是否是优惠，优惠计算优惠价格
            var goodsPromotionBase = goods.goodsPromotionBase;
            cartGood.discountGoods = false;
            //cartGood.single = false;
            cartGood.cartGoods = true;
            if (goodsPromotionBase) {
                if (goodsPromotionBase.type == 1) {
                    cartGood.currentFee = cartGood.currentFee * goodsPromotionBase.discount / 10;
                } else if (goodsPromotionBase.type == 2) {
                    cartGood.currentFee -= goodsPromotionBase.discount;
                }
                cartGood.skuid = goodsPromotionBase.promotionid;
                cartGood.discountGoods = true;
                //cartGood.single = true;
            }
            shop_goods_list[shopid].push(cartGood);
        } else {
            if(goodsitem.discountGoods&&goodsitem.num>1){
                input_alert("该美食限1份优惠，超过以原价计算哦",3000,"#prompt",100);
            }
            if (goodsitem.num < goods.stock) {
                goodsitem.num++;
            } else {
                input_alert("超出库存！");
                return;
            }
        }
    };
    this.removeBarcode = function (shopid, goods) {
        var goodsList = shop_goods_list[shopid];
        var goodsitem = null;
        for (var i = 0; i < goodsList.length; i++) {
            goodsitem = goodsList[i];
            if (goodsitem.goodsid == goods.goodsid) {
                if (goods.skuid) {
                    if (goodsitem.skuid == goods.skuid) {
                        goodsNum(goodsitem, goodsList, i);
                        break;
                    }
                } else {
                    goodsNum(goodsitem, goodsList, i);
                    break;
                }
            }
        }
        if (goodsList.length == 0) {
            delete shop_goods_list[shopid];
        } else {
            shop_goods_list[shopid] = goodsList;
        }
    };
    //减少购买
    this.removeGood = function (shopid, groupid, goods) {
        var goodsList = shop_goods_list[shopid];
        var goodsitem = null;
        for (var i = 0; i < goodsList.length; i++) {
            goodsitem = goodsList[i];
            if (goodsitem.goodsid == goods.goodsid && goodsitem.groupid == groupid) {
                if (goods.skuid) {
                    if (goodsitem.skuid == goods.skuid) {
                        goodsNum(goodsitem, goodsList, i);
                        break;
                    }
                } else {
                    goodsNum(goodsitem, goodsList, i);
                    break;
                }
            }
        }
        this.getGoodsDetail(shopid, groupid, goods.goodsid, 'remove');
        if (goodsList.length == 0) {
            delete shop_goods_list[shopid];
        } else {
            shop_goods_list[shopid] = goodsList;
        }
    };
    //判断优惠商品数量大于1先减少未优惠部分
    function goodsNum(goodsitem, goodsList, i) {
        goodsitem.num--;
        if (goodsitem.num < 1) {
            goodsList.splice(i, 1)
        }
    }

    //通过订单重新购买
    this.addGoodByOrder = function (shopid, goodsid, num, frequency, skuid) {
        var goodsList = menuList[shopid];
        if (frequency == 0) {
            shop_goods_list[shopid] = [];
            for (var i = 0; i < goodsList.length; i++) {
                var goods = goodsList[i];
                goods.num = 0;
                for (var j = 0; j < goods.goods.length; j++) {
                    var good = goods.goods[j];
                    good.num = 0;
                    good.groupid = goods.groupid;
                }
            }
        }
        var select_good = null;
        for (var i = 0; i < goodsList.length; i++) {
            var goods = goodsList[i];
            for (var j = 0; j < goods.goods.length; j++) {
                var good = goods.goods[j];
                if (good.goodsid == goodsid) {
                    goods.num += num;
                    if (num <= good.stock) {
                        good.num += num;
                    } else {
                        good.num += good.stock;
                    }
                    good.shopid = shopid;
                    good.images = "";
                    good.state = 0;
                    good.cartid = "";
                    select_good = good;
                    break;
                }
            }
            //监听到商品已经添加跳出循环
            if (select_good) {
                break;
            }
        }
        var cartGood = {};
        for (var name in select_good) {
            cartGood[name] = select_good[name];
        }
        cartGood.num = num;
        //商品有sku的情况
        if (skuid) {
            for (var j = 0; j < select_good.goodsSkuList.length; j++) {
                var sku = select_good.goodsSkuList[j];
                if (skuid == sku.skuid) {
                    cartGood.price = sku.price;
                    cartGood.skuid = sku.skuid;
                    cartGood.properties = sku.properties;
                    break
                }
            }
            if(select_good.goodsPromotionBase){
                var goodsPromotionBase = select_good.goodsPromotionBase;
                if(skuid == goodsPromotionBase.promotionid){
                    if (goodsPromotionBase.type == 1) {
                        cartGood.currentFee = cartGood.currentFee * goodsPromotionBase.discount / 10;
                    } else if (goodsPromotionBase.type == 2) {
                        cartGood.currentFee -= goodsPromotionBase.discount;
                    }
                    cartGood.skuid = goodsPromotionBase.promotionid;
                    cartGood.discountGoods = true;
                }
            }
        }
        shop_goods_list[shopid].push(cartGood);
    };
    //再来一单功能(未使用)
    this.anotherOneOrder = function (shopid, goodsid, num, frequency, skuid) {
        var goodsList = menuList[shopid];
        if (frequency == 0) {
            shop_goods_list[shopid] = [];
            for (var i = 0; i < goodsList.length; i++) {
                var goods = goodsList[i];
                goods.num = 0;
                for (var j = 0; j < goods.goods.length; j++) {
                    var good = goods.goods[j];
                    good.num = 0;
                }
            }
        }
        for (var i = 0; i < goodsList.length; i++) {
            var goods = goodsList[i];
            for (var j = 0; j < goods.goods.length; j++) {
                var good = goods.goods[j];
                if (good.goodsid == goodsid) {
                    good.num += num;
                    good.shopid = shopid;
                    good.images = "";
                    good.state = 0;
                    good.cartid = "";
                    break;
                }
            }
        }
        var cartGood = {};
        for (var name in good) {
            cartGood[name] = good[name];
        }
        cartGood.num = num;
        //商品有sku的情况
        if (skuid) {
            for (var j = 0; j < good.goodsSkuList.length; j++) {
                var sku = good.goodsSkuList[j];
                if (skuid == sku.skuid) {
                    cartGood.price = sku.price;
                    cartGood.skuid = sku.skuid;
                    cartGood.properties = sku.properties;
                    break
                }
            }
        }
        shop_goods_list[shopid].push(cartGood);
    };
    //获取总价和产品总数
    this.gettotalpriceandcount = function (shopid) {
        var count = 0,integration = 0,primecost= 0,totalprice = 0;
        var goods_list = [];
        var discount = false;
        if (shop_goods_list[shopid]) {
            goods_list = shop_goods_list[shopid];
        }
        for (var i = 0; i < goods_list.length; i++) {
            var goodsitem = goods_list[i];
            if (goodsitem.num > 0) {
                if (goodsitem.discountGoods) {
                    discount = true;
                    totalprice += (goodsitem.price * goodsitem.num-(goodsitem.price-goodsitem.currentFee));
                }else{
                    totalprice += goodsitem.price * goodsitem.num;
                }
                primecost += goodsitem.price * goodsitem.num;
                count += 1 * goodsitem.num;
                integration += (goodsitem.price * goodsitem.num) / 100;
            }
        }
        return {hadDiscount: discount, totalprice: totalprice,primecost:primecost, count: count, integration: integration};
    };

    //获取店铺购物列表
    this.getShopCarByShopid = function (shopid) {
        var goods_list = [];
        if (shop_goods_list[shopid]) {
            goods_list = shop_goods_list[shopid];
        }
        return goods_list;
    };
    //移除某个id的商品
    /*this.cleanGoodById = function (shopid, groupid, goodsid) {
     var goodsList = menuList[shopid];
     if (goodsList == undefined) {
     goodsList = shop_goods_list[shopid];
     }
     var goodsitem = null;
     for (var i = 0; i < goodsList.length; i++) {
     goodsitem = goodsList[i];
     if (goodsitem.goods == undefined && goodsitem.goodsid == goodsid) {
     goodsitem.num = 0;
     break;
     }
     if (goodsitem.goods != undefined && goodsitem.groupid == groupid) {
     break;
     }
     }
     if (goodsitem.goods != undefined) {
     for (var j = 0; j < goodsitem.goods.length; j++) {
     var good = goodsitem.goods[j];
     if (good.goodsid == goodsid) {
     good.num = 0;
     break;
     }
     }
     }
     };*/
    //移除某个商店所有的商品信息
    this.removeShopById = function (shopid) {
        var goodsList = shop_goods_list[shopid];
        var goods_list = menuList[shopid];
        var goodsitem = null;
        //该店铺没有商品直接return
        if(!goodsList){
            return;
        }
        for (var i = goodsList.length - 1; i >= 0; i--) {
            goodsList.splice(i, 1);
        }
        for (var i = 0; i < goods_list.length; i++) {
            goodsitem = goods_list[i];
            for (var j = 0; j < goodsitem.goods.length; j++) {
                var good = goodsitem.goods[j];
                good.num = 0;
            }
            goodsitem.num = 0;
        }
        delete shop_goods_list[shopid];
    };
    //店铺所有商品立即购买操作
    this.buyNowShopById = function (shopid) {
        var goodscontent = this.gettotalpriceandcount(shopid);
        var shopinfo = ShopService.queryShopByShopid(shopid);
        var list = [];
        //外卖项目只有单店订单
        order_list = [];
        if(!shop_goods_list[shopid]){
            return null;
        }
        for(var i= 0;i<shop_goods_list[shopid].length;i++){
            var goods = shop_goods_list[shopid][i];
            if(goods.discountGoods&&goods.num>1){
                //有折扣商品第一件为折扣价格其他的为原价
                var goods1 = {};
                var goods2 = {};
                for(var name in goods){
                    goods1[name] = goods[name];
                } for(var name in goods){
                    goods2[name] = goods[name];
                }
                goods1.num = 1;
                goods1.skuid = 1;
                goods2.num -=1;
                goods2.discountGoods =false;
                goods2.currentFee =goods2.price;
                list.push(goods1);
                list.push(goods2);
            }else{
                list.push(goods);
            }
        }
        if (list.length>0) {
            var shop_goods = {
                shopid: shopid,
                shop: shopinfo,
                list: list,
                goodscontent: goodscontent,
                buyWay: "立即购买",
            };
            var index = -1;
            for(var i=0;i<order_list.length;i++){
                if(order_list[i].shopid == shopid){
                    index = i;
                    break;
                }
            }
            if(index>-1){
                order_list.splice(index,1,shop_goods);
            }else{
                order_list.push(shop_goods);
            }
        }
        return order_list;
    };
    //获取商品详情
    this.getGoodsDetail = function (shopid, groupid, goodsid, addCut) {
        var goodsList = menuList[shopid];
        var goodsitem = null;
        for (var i = 0; i < goodsList.length; i++) {
            if (groupid == goodsList[i].groupid) {
                goodsitem = goodsList[i];
                break;
            }
        }
        //当前传入数据是店铺列表继续执行操作
        var good = null;
        for (var j = 0; j < goodsitem.goods.length; j++) {
            good = goodsitem.goods[j];
            if (good.goodsid == goodsid) {
                if (addCut == 'add') {
                    if (good.num < good.stock) {
                        goodsitem.num++;
                        good.num++;
                    }
                } else if (addCut == 'remove') {
                    goodsitem.num--;
                    good.num--;
                }
                good.groupid = groupid;
                good.shopid = shopid;
                good.currentFee = good.price;
                good.images = "";
                good.state = 0;
                good.cartid = "";
                break;
            }
        }
        return {goods: good}
    };
    //获取商品购买数量
    this.getBuycountByGoodsid = function (shopid, groupid, goodsid) {
        var goods_list = [];
        if (shop_goods_list[shopid]) {
            goods_list = shop_goods_list[shopid];
        }
        for (var i = 0; i < goods_list.length; i++) {
            var goodsitem = goods_list[i];
            if (goodsitem.groupid == groupid && goodsitem.goodsid == goodsid) {
                return goodsitem.num;
            }
        }
        return 0;
    }
}
